package org.example.outputformat;


import org.apache.hadoop.fs.FSDataOutputStream;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.IOUtils;
import org.apache.hadoop.io.NullWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.RecordWriter;
import org.apache.hadoop.mapreduce.TaskAttemptContext;

import java.io.IOException;


public class LogRecordWriter extends RecordWriter<Text, NullWritable> {

    private final FSDataOutputStream fengliulinOut;
    private final FSDataOutputStream otherOut;

    public LogRecordWriter(TaskAttemptContext job) {
        try {
            FileSystem fs = FileSystem.get(job.getConfiguration());
            fengliulinOut = fs.create(new Path("/Users/fengliulin/Documents/Testsss/1.log"));

            otherOut = fs.create(new Path("/Users/fengliulin/Documents/Testsss/2.log"));
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public void write(Text key, NullWritable value) throws IOException, InterruptedException {
        String log = key.toString();
        if (log.contains("fengliulin")) {
            fengliulinOut.writeBytes(log);
        } else {
            otherOut.writeBytes(log);
        }
    }

    @Override
    public void close(TaskAttemptContext context) throws IOException, InterruptedException {
        IOUtils.closeStream(fengliulinOut);
        IOUtils.closeStream(otherOut);
    }
}

